﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class JinjuziLogin : LoginImplBase
    {
        public JinjuziLogin() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            string loginurl = "http://as.jinjuzi.com/login_ajax.php";
            string data = String.Format("account={0}&password={1}", _username, _password);
            HttpResult resp = TransferMgr.doPostPure(loginurl, data, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();

            if (retstr.IndexOf("fail") >= 0)
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            // 首先查找游戏地址
            string gamelist_url = "http://as.jinjuzi.com/index.php";
            resp = TransferMgr.doGetPure(gamelist_url, ref cookies);
            string content = resp.getContent();
            string gameString = String.Format("双线{0}区", translateNumber(_acc.ServerId));
            Regex re = new Regex(String.Format("<a.*href=\"([^\"']*?)\".*>{0}.*</a>", gameString));
            Match m = re.Match(content);
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }

            string redirecturl = m.Groups[1].Value;
            if (redirecturl.StartsWith("./")) redirecturl = redirecturl.Substring(1);
            if (!redirecturl.StartsWith("http")) redirecturl = "http://as.jinjuzi.com" + redirecturl;

            resp = TransferMgr.doGetPure(redirecturl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }
            string location = resp.getExtraHeader("Location");
            if (location == null) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }
            if (!location.StartsWith("http")) location = "http://as.jinjuzi.com/" + location;

            resp = TransferMgr.doGetPure(location, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }
            content = resp.getContent();
            re = new Regex(".*?url=(.*?)'");
            m = re.Match(content);
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }
            string gotourl = m.Groups[1].Value;
            if (!gotourl.StartsWith("http")) gotourl = "http://as.jinjuzi.com/" + gotourl;

            resp = TransferMgr.doGetPure(gotourl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }
            content = resp.getContent();
            re = new Regex("<frame src=\"(http.*?start\\.action.*?)\".*");
            m = re.Match(content);
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }
            string gameurl = m.Groups[1].Value;

            processStartGame(gameurl, result, ref cookies);
            return result;
        }
        private string translateNumber(int number)
        {
            string[] numbers = new string[10] { "", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            string result = number.ToString();
            if (number < 10) result = numbers[number];
            else result = number.ToString();
            return result;
        }
    }
}
